﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.IO;
using System.Web.UI;
using System.Web.Configuration;
using CodeTunnel.MvcUtilities.Objects;
using CodeTunnel.MvcUtilities.Utilities;
using System.Linq.Expressions;
using System.Web.Mvc.Html;

namespace CodeTunnel.MvcUtilities.Helpers
{
    /// <summary>
    /// A library of useful HtmlHelpers.
    /// </summary>
    public static class HtmlHelpers
    {
        /// <summary>
        /// HTML helper to transform Markdown syntax into valid HTML.asfas
        /// </summary>
        /// <param name="helper">The current HtmlHelper.</param>
        /// <param name="text">The text to be transformed.</param>
        /// <returns>MvcHtmlString</returns>
        public static MvcHtmlString Markdown(this HtmlHelper helper, string text, bool sanitize)
        {
            string html = MarkdownUtils.FormatMarkdown(text, sanitize);
            return MvcHtmlString.Create(html);
        }

        /// <summary>
        /// Generates HTML paging links based on the specified PagingInfo.
        /// </summary>
        /// <param name="html">The current HtmlHelper.</param>
        /// <param name="pagingInfo">The paging metadata to be used when creating the links.</param>
        /// <param name="pageUrl">A function to return a valid URL with correct page number.</param>
        /// <returns>MvcHtmlString</returns>
        public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl)
        {
            StringBuilder result = new StringBuilder();
            result.Append("<div class='pageLinks'>");
            if (pagingInfo.CurrentPage > 1)
            {
                result.Append(string.Format("<a href='{0}'>{1}</a>", pageUrl(1), "&lt;&lt;first"));
                result.Append(string.Format("<a href='{0}'>{1}</a>", pageUrl(pagingInfo.CurrentPage - 1), "&lt;prev"));
            }
            for (int i = 1; i <= pagingInfo.TotalPages; i++)
            {
                string selectedClass = string.Empty;
                if (pagingInfo.CurrentPage == i)
                    selectedClass = "class='pageLinkSelected'";
                string tag = string.Format("<a href='{0}' {1}>{2}</a>", pageUrl(i), selectedClass, i.ToString());
                result.Append(tag);
            }
            if (pagingInfo.CurrentPage < pagingInfo.TotalPages)
            {
                result.Append(string.Format("<a href='{0}'>{1}</a>", pageUrl(pagingInfo.CurrentPage + 1), "next&gt;"));
                result.Append(string.Format("<a href='{0}'>{1}</a>", pageUrl(pagingInfo.TotalPages), "last&gt;&gt;"));
            }
            result.Append("</div>");
            return MvcHtmlString.Create(result.ToString());
        }

        /// <summary>
        /// Helper to create a drop down list pre-populated with all 50 states.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="html">The helper class to extend.</param>
        /// <param name="expression">Lambda for binding the drop down to a model property.</param>
        /// <returns>A standard drop down list pre-populated with all 50 states.</returns>
        public static MvcHtmlString StateDropDownListFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
        {
            Dictionary<string, string> stateList = new Dictionary<string, string>()
            {
                {"None","Please Choose"},
                {"AL", "Alabama"},
                {"AK", "Alaska"},
                {"AZ", "Arizona"},
                {"AR", "Arkansas"},
                {"CA", "California"},
                {"CO", "Colorado"},
                {"CT", "Connecticut"},
                {"DE", "Delaware"},
                {"FL", "Florida"},
                {"GA", "Georgia"},
                {"HI", "Hawaii"},
                {"ID", "Idaho"},
                {"IL", "Illinois"},
                {"IN", "Indiana"},
                {"IA", "Iowa"},
                {"KS", "Kansas"},
                {"KY", "Kentucky"},
                {"LA", "Louisiana"},
                {"ME", "Maine"},
                {"MD", "Maryland"},
                {"MA", "Massachusetts"},
                {"MI", "Michigan"},
                {"MN", "Minnesota"},
                {"MS", "Mississippi"},
                {"MO", "Missouri"},
                {"MT", "Montana"},
                {"NE", "Nebraska"},
                {"NV", "Nevada"},
                {"NH", "New Hampshire"},
                {"NJ", "New Jersey"},
                {"NM", "New Mexico"},
                {"NY", "New York"},
                {"NC", "North Carolina"},
                {"ND", "North Dakota"},
                {"OH", "Ohio"},
                {"OK", "Oklahoma"},
                {"OR", "Oregon"},
                {"PA", "Pennsylvania"},
                {"RI", "Rhode Island"},
                {"SC", "South Carolina"},
                {"SD", "South Dakota"},
                {"TN", "Tennessee"},
                {"TX", "Texas"},
                {"UT", "Utah"},
                {"VT", "Vermont"},
                {"VA", "Virginia"},
                {"WA", "Washington"},
                {"WV", "West Virginia"},
                {"WI", "Wisconsin"},
                {"WY", "Wyoming"}
            };
            return html.DropDownListFor(expression, new SelectList(stateList, "key", "value"));
        }
    }
}
